package com.qijian.maindata.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qijian.common.annotation.Log;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.core.page.TableDataInfo;
import com.qijian.common.enums.BusinessType;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.qijian.common.utils.poi.ExcelUtil;
import com.qijian.maindata.domain.Store;
import com.qijian.maindata.domain.query.StoreQuery;
import com.qijian.maindata.service.IStoreService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 仓库Controller
 *
 * @author ruoyi
 * @date 2021-08-18
 */
@Api(tags = "仓库")
@RestController
@RequiredArgsConstructor
@RequestMapping("/business/store")
public class StoreController extends BaseController {
    private final IStoreService storeService;

    /**
     * 查询仓库列表
     */
    @PreAuthorize("@ss.hasPermi('business:store:list')")
    @GetMapping("/list")
    public TableDataInfo list(StoreQuery query, Page<Store> page) {
        return getDataTable(storeService.page(page, WhereEntityTool.invoke(query)));
    }

    /**
     * 查询所有仓库列表
     */
    @PreAuthorize("@ss.hasAnyPermi('business:store:list,business:storeItem:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll(StoreQuery query) {
        List<Store> listData = storeService.list(WhereEntityTool.invoke(query));
        return getDataTable(listData);
    }

    /**
     * 导出仓库列表
     */
    @PreAuthorize("@ss.hasPermi('business:store:export')")
    @Log(title = "仓库", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(StoreQuery query) {
        return new ExcelUtil<>(Store.class).exportExcel(storeService.list(WhereEntityTool.invoke(query)), "仓库数据");
    }

    /**
     * 获取仓库详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:store:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(storeService.getById(id));
    }

    /**
     * 新增仓库
     */
    @PreAuthorize("@ss.hasPermi('business:store:add')")
    @Log(title = "仓库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Store store) {
        store.setCode("CK-" + String.format("%04d", storeService.countAll()));
        return toAjax(storeService.save(store));
    }

    /**
     * 修改仓库
     */
    @PreAuthorize("@ss.hasPermi('business:store:edit')")
    @Log(title = "仓库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Store store) {
        return toAjax(storeService.updateById(store));
    }

    /**
     * 删除仓库
     */
    @PreAuthorize("@ss.hasPermi('business:store:remove')")
    @Log(title = "仓库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(storeService.removeByIds(ids));
    }
}
